import java.applet.*;
import java.awt.*;

public class BouncingBall extends Applet implements Runnable
{
 private int x,y,xx,yy,rl,rh;
 private Color c,cb;
 boolean frozen=false;
 private Graphics g;
 Thread animatorThread;

 public void init()
{
 rl=250;
 rh=276;
 x=(int)Math.round(Math.random()*(rl-20)+10);
 xx=(int)Math.round(Math.random()*5);
 y=(int)Math.round(Math.random()*(rh-20)+10);
 yy=(int)Math.round(Math.random()*5);
 cb=Color.white;
 c=Color.blue; 
}
 
 public void start()
{
 if (!frozen)
{ 
 if (animatorThread == null) animatorThread = new Thread(this);
 animatorThread.start();
} 
}
 
 public void stop()
{
 animatorThread=null;
}
    
 public boolean mouseDown(Event e, int x, int y) 
{
  if (frozen) 
{
     frozen = false;
     start();
}
 else 
{
     frozen = true;
     stop();
}
 return true;
} 

 public void paint(Graphics g)
{
 g.setColor(cb);
 g.fillRect(0,0,rl,rh);
 g.setColor(c);
 g.fillOval(x,y,10,10);
}
 
 public void run()
{
  while (Thread.currentThread()==animatorThread)
{
  paint(this.getGraphics());
  if ( x<(rl-10) && x >10) x+=xx;
  else {xx*=-1;x+=xx;}  
  if ( y<(rh-10) && y >10) y+=yy;
  else {yy*=-1;y+=yy;}  
  for (int j=1;j<1000000;j++);
}
}

} 
